package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.mapper.CourtStatisticMapper;
import com.ruoyi.reservationserver.dao.ReservationsDao;
import com.ruoyi.system.domain.CourtStatistic;
import com.ruoyi.reservationserver.entity.User;
import com.ruoyi.reservationserver.pojo.CourtStatisticPojo;
import com.ruoyi.reservationserver.service.CourtService;
import com.ruoyi.system.service.CourtStatisticService;
import com.ruoyi.reservationserver.service.ReservationsService;
import com.ruoyi.reservationserver.service.UserService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 球场数据统计表
(CourtStatistic)表服务实现类
 *
 * @author makejava
 * @since 2023-07-04 16:21:48
 */
@Service("courtStatisticService")
public class CourtStatisticServiceImpl extends ServiceImpl<CourtStatisticMapper, CourtStatistic> implements CourtStatisticService {
    @Resource
    private CourtStatisticMapper courtStatisticMapper;

    @Resource
    private CourtStatisticService courtStatisticService;

    @Resource
    private UserService userService;

    @Resource
    private CourtService courtService;

    @Resource
    private ReservationsService reservationsService;

    @Resource
    private ReservationsDao reservationsDao;




    /**
     * 普通管理員总統計数量
     * @return
     */
    @Override
    public CourtStatisticPojo getTotalSum() {
        CourtStatisticPojo statisticPojo = new CourtStatisticPojo();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("is_member","0");
        int memberNum = userService.count(queryWrapper);
        int AllNum = userService.count();
        int userNum=AllNum-memberNum;
        int courtNum =  courtService.count();
        int reservationNum= reservationsDao.getReservationNumSum();
        statisticPojo.setCourtNum(courtNum);
        statisticPojo.setReservationNum(reservationNum);
        statisticPojo.setUserNum(userNum);
        statisticPojo.setMemberNum(memberNum);
        return statisticPojo;
    }

    @Override
    public int totalUser() {
        int count = userService.count();
        return count;
    }




    /**
     * 通过ID查询单条数据
     *
     * @param courtStatisticId 主键
     * @return 实例对象
     */
    @Override
    public CourtStatistic queryById(Integer courtStatisticId) {
        return this.courtStatisticMapper.queryById(courtStatisticId);
    }

    /**
     * 分页查询
     *
     * @param courtStatistic 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<CourtStatistic> queryByPage(CourtStatistic courtStatistic, PageRequest pageRequest) {
        long total = this.courtStatisticMapper.count(courtStatistic);
        return new PageImpl<>(this.courtStatisticMapper.queryAllByLimit(courtStatistic, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param courtStatistic 实例对象
     * @return 实例对象
     */
    @Override
    public CourtStatistic insert(CourtStatistic courtStatistic) {
        this.courtStatisticMapper.insert(courtStatistic);
        return courtStatistic;
    }

    /**
     * 修改数据
     *
     * @param courtStatistic 实例对象
     * @return 实例对象
     */
    @Override
    public CourtStatistic update(CourtStatistic courtStatistic) {
        this.courtStatisticMapper.update(courtStatistic);
        return this.queryById(courtStatistic.getCourtStatisticId());
    }

    /**
     * 通过主键删除数据
     *
     * @param courtStatisticId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer courtStatisticId) {
        return this.courtStatisticMapper.deleteById(courtStatisticId) > 0;
    }
}
